// space-buccaneer
//
// playstate.cpp


// includes
#include <SFML/Graphics.hpp>
#include "gamestate.h"
#include "engine.h"
#include "playstate.h"
#include "xmlParser.h"
#include <iostream>
#include <cmath>
#include <stdio.h>

CPlayState CPlayState::m_PlayState;

void CPlayState::Init()
{
    // this open and parse the XML file:
    XMLNode xMainNode=XMLNode::openFileHelper("xml/map0000.xml","sector");

    XMLNode xNode = xMainNode.getChildNode("map");

    std::cout << "x: " << xNode.getAttribute("x") << " "
              << "y: " << xNode.getAttribute("y") << " "
              << "Name: " << xNode.getAttribute("name") << std::endl;

    xNode=xMainNode.getChildNode("planets");
    int n = xNode.nChildNode("planet");

    std::cout << "planets: " << n << std::endl;
    for (int i = 0; i < n; i++)
        std::cout << "name: " << xNode.getChildNode("planet", i).getAttribute("name") << std::endl;

    m_tex_methaneice.loadFromFile("images/methane-ice.png");
    m_tex_methanebarren.loadFromFile("images/methane-barren.png");
    m_tex_barrencharred.loadFromFile("images/barren-charred.png");
    m_tex_barrenicy.loadFromFile("images/barren-icy.png");
    m_tex_barren.loadFromFile("images/barren.png");

    m_tex_w1x1.loadFromFile("images/whitestar_1x1.png");
    m_tex_w3x3.loadFromFile("images/whitestar_3x3.png");

    go_list.push_back(new go);
    go_list.back()->spr.setTexture(m_tex_methaneice);
    go_list.back()->xu = 0;
    go_list.back()->yu = 0;

    go_list.push_back(new go);
    go_list.back()->spr.setTexture(m_tex_methanebarren);
    go_list.back()->xu = 944;
    go_list.back()->yu = 944;

    go_list.push_back(new go);
    go_list.back()->spr.setTexture(m_tex_barrencharred);
    go_list.back()->xu = 1889;
    go_list.back()->yu = 1889;

    go_list.push_back(new go);
    go_list.back()->spr.setTexture(m_tex_barrenicy);
    go_list.back()->xu = 1889;
    go_list.back()->yu = 0;

    go_list.push_back(new go);
    go_list.back()->spr.setTexture(m_tex_barren);
    go_list.back()->xu = 0;
    go_list.back()->yu = 1889;

    m_tex_pirate1.loadFromFile("images/pirate1.png");
    m_spr_pirate1.setTexture(m_tex_pirate1);
    m_spr_pirate1.setOrigin(11, 15);
    m_spr_pirate1.setPosition(400, 300);

    m_tex_end.loadFromFile("images/the_end.png");

    for(int i = 0; i < 100; i++)
    {
        go_list.push_back(new go);
        go_list.back()->spr.setTexture(m_tex_end);
        go_list.back()->xu = i*20;
        go_list.back()->yu = 0;

        go_list.push_back(new go);
        go_list.back()->spr.setTexture(m_tex_end);
        go_list.back()->xu = i*20;
        go_list.back()->yu = 1979;

        go_list.push_back(new go);
        go_list.back()->spr.setTexture(m_tex_end);
        go_list.back()->xu = 0;
        go_list.back()->yu = i*20;

        go_list.push_back(new go);
        go_list.back()->spr.setTexture(m_tex_end);
        go_list.back()->xu = 1979;
        go_list.back()->yu = i*20;
    }

    for (int i = 0; i < 20; i++)
    {
        stars_list.push_back(new go);
        stars_list.back()->spr.setTexture(m_tex_w3x3);
        stars_list.back()->spr.setPosition(i*10, 300);
        stars_list.back()->xu = i * 3;
        stars_list.back()->yu = 300;
    }

    turn = 0;
    speed = 0;
    piover180 = 3.1415926535897932384626433832795/180;


    view_x = 0;
    view_y = 0;
    max_view_x = 2000;
    max_view_y = 2000;
    lastTime = clock_fps.restart().asSeconds();
    currentTime = 0;
    fps = 0;

    clock.restart();
}

void CPlayState::Cleanup()
{

}

void CPlayState::Pause()
{
}

void CPlayState::Resume()
{

}

void CPlayState::HandleEvents(CGameEngine* game)
{
    // Process events
    sf::Event Event;
    while (game->App->pollEvent(Event))
    {
        // Close window : exit
        if (Event.type == sf::Event::Closed)
            game->Quit();

        // ESC has been pressed
        if (Event.type == sf::Event::KeyPressed)
        {
            if (Event.key.code == sf::Keyboard::Escape)
            {
                FadeOut(game);
                game->Quit();
            }

            if (Event.key.code == sf::Keyboard::A)
            {
                turn = -1;
            }

            if (Event.key.code == sf::Keyboard::D)
            {
                turn = 1;
            }

            if (Event.key.code == sf::Keyboard::W)
            {
                if (speed < 4)
                    speed++;
            }

            if (Event.key.code == sf::Keyboard::S)
            {
                if (speed > 0)
                    speed--;
            }
        }

        if (Event.type == sf::Event::KeyReleased)
        {
            if (Event.key.code == sf::Keyboard::A || Event.key.code == sf::Keyboard::D)
                turn = 0;
        }

        if (Event.type == sf::Event::MouseMoved)
        {
        }
    }
}

void CPlayState::Update(CGameEngine* game)
{
    sf::Time time = clock.restart();
    float delta_time = time.asSeconds()*100;

    float tmp_angle = m_spr_pirate1.getRotation() + turn*delta_time;

    m_spr_pirate1.setRotation(tmp_angle);

    view_x += (std::sin(tmp_angle*piover180)*speed)*delta_time;
    view_y += (std::cos(tmp_angle*piover180)*speed)*delta_time*-1;

    if (view_x < 0)
    {
        view_x = 0;
        speed = 0;
    }

    if (view_x > max_view_x - 800)
    {
        view_x = max_view_x - 800;
        speed = 0;
    }

    if (view_y < 0)
    {
        view_y = 0;
        speed = 0;
    }

    if (view_y > max_view_y - 600)
    {
        view_y = max_view_y - 600;
        speed = 0;
    }
    fps++;
    if (clock_fps.getElapsedTime().asSeconds() > 1)
    {
        currentTime = clock_fps.restart().asSeconds();
        std::cout << "fps :" << fps << std::endl;
        fps = 0;
    }
}

void CPlayState::Draw(CGameEngine* game)
{
    game->App->clear (sf::Color (0, 0, 0));

    for (i_stars = stars_list.begin (); i_stars != stars_list.end (); i_stars++)
        game->App->draw((*i_stars)->spr);

    for (i_go = go_list.begin (); i_go != go_list.end (); i_go++)
        CheckDraw((*i_go), game);

    game->App->draw (m_spr_pirate1);

    game->App->display ();
}

void CPlayState::FadeOut (CGameEngine* game)
{
    for (int i = 0; i < 255; i++)
    {
        //m_SpriteBg.setColor (sf::Color (255, 255, 255, 255-i));

        Draw (game);
    }
}

bool CPlayState::CheckDraw (go* g, CGameEngine* game)
{
    float leftA, leftB;
    float rightA, rightB;
    float topA, topB;
    float bottomA, bottomB;

    //Calculate the sides of rect A
    leftB = g->xu;
    rightB = leftB + g->spr.getTextureRect().width;
    topB = g->yu;
    bottomB = topB + g->spr.getTextureRect().height;

    //Calculate the sides of rect B
    leftA = view_x;
    rightA = leftA + 800;
    topA = view_y;
    bottomA = topA + 600;

     //If any of the sides from A are outside of B
    if( bottomA <= topB )
    {
        return false;
    }

    if( topA >= bottomB )
    {
        return false;
    }

    if( rightA <= leftB )
    {
        return false;
    }

    if( leftA >= rightB )
    {
        return false;
    }

    g->spr.setPosition(leftB-leftA, topB-topA);
    game->App->draw(g->spr);

    return true;
}
